package online.hxstar.learn.udp;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioDatagramChannel;

import online.hxstar.learn.util.AppUtils;

/**
 * @author huxing
 * @date 2018-12-18 15:55
 */
public class ChineseProverbServer {
    public static void main(String[] args) throws InterruptedException {
        int port = AppUtils.getPort(args);
        new ChineseProverbServer().bin(port);
    }

    private void bin(int port) throws InterruptedException {

        EventLoopGroup group = new NioEventLoopGroup();
        try {
            Bootstrap bootstrap = new Bootstrap();
            bootstrap.group(group)
                    .channel(NioDatagramChannel.class)
                    .option(ChannelOption.SO_BROADCAST, true)
                    .handler(new ChineseProverbServerHandler());
            bootstrap.bind(port).sync().channel().closeFuture().await();
        } finally {
            group.shutdownGracefully();
        }
    }
}
